﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace ProjectQuaDep.Admin
{
    public partial class SqlCommand : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["SQLExe"] == null)
            {
                Session["SQLExe"] = false;
            }
            if (Session["SQLExe"].Equals(true))
            {
                pnlog.Visible = false;
                pnExe.Visible = true;
            }
            else
            {
                pnlog.Visible = true;
                pnExe.Visible = false;
                pnView.Visible = false;
            }
        }
        protected void CmdDangNhap_Click(object sender, EventArgs e)
        {
            if ((TxtTen.Text.Equals("lamdaica")) && (txtMatKhau.Text.Equals("lamdaica123!")))
            {
                Session["SQLExe"] = true;
                pnlog.Visible = false;
                pnExe.Visible = true;
            }
            else
            {
                Session["SQLExe"] = false;
                pnlog.Visible = true;
                pnExe.Visible = false;
            }
        }
        protected void btnExe_Click(object sender, EventArgs e)
        {
            lblInfo.Text = Server.MapPath("/");
            string sttemp;
            sttemp = "";
            sttemp = txtSql.Text.Trim();
            try
            {
                sttemp = sttemp.Substring(0, 6);
            }
            catch
            {

            }

            sttemp = sttemp.ToLower();
            if (sttemp.Equals("select"))
            {
                pnView.Visible = true;
                BindData(txtSql.Text.Trim());
            }
            else
            {
                pnView.Visible = false;
                fnsqlexe(txtSql.Text.Trim());
            }
        }
        void BindData(string stsql)
        {
            try
            {
                DataTable dt = new DataTable();
                dt = DBUtil.SelectTable(stsql);
                gvData.DataSource = dt;
                gvData.DataBind();
                lblSql.Text = "Thực hiện thành công !!!";
            }
            catch (Exception ex)
            {
                lblSql.Text = "Lỗi thi hành lệnh : " + ex.Message;
            }
        }
        void fnsqlexe(string stsql)
        {
            try
            {
                DBUtil.Execute(stsql);
                lblSql.Text = "Thực hiện thành công !!!";
            }
            catch (Exception ex)
            {
                lblSql.Text = "Lỗi thi hành lệnh : " + ex.Message;
            }

        }
    }
}